package server;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * Minicat的主类
 */
public class Bootstrap {

    private SimpleServer simpleServer;

    /**
     * 初始化
     */
    public void init() {
        try {
            //1. 加载解析server.xml，初始化Host和Context
            InputStream serverInputStream = this.getClass().getClassLoader().getResourceAsStream("server.xml");
            SAXReader ServerSaxReader = new SAXReader();

            Document serverDocument = ServerSaxReader.read(serverInputStream);
            Element rootElement = serverDocument.getRootElement();

            /**
             * HOST
             */
            Element hostElement = (Element)rootElement.selectSingleNode("/Server/Service/Engine/Host");

            SimpleHost simpleHost = new SimpleHost();
            String name = hostElement.attribute("name").getValue();
            String appBase = hostElement.attribute("appBase").getValue();

            simpleHost = new SimpleHost();
            simpleHost.setName(name);
            simpleHost.setAppBase(appBase);

            Map<String, SimpleContext> simpleContextMap = new HashMap<>();
            List<Element> contextElements = hostElement.elements("Context");
            for (Element contextElement : contextElements) {
                String docBase = contextElement.attributeValue("docBase");
                String path = contextElement.attributeValue("path");

                SimpleContext simpleContext = new SimpleContext();
                simpleContext.setDocBase(docBase);
                simpleContext.setPath(path);

                //
                simpleContext.initServlet(appBase);

                simpleContextMap.put(path, simpleContext);
            }

            simpleHost.setSimpleContextMap(simpleContextMap);

            /**
             * Connector
             */
            Element connectorElement = (Element)rootElement.selectSingleNode("/Server/Service/Connector");
            String portString = connectorElement.attributeValue("port");
            int port = Integer.valueOf(portString);
            SimpleConnector simpleConnector = new SimpleConnector();
            simpleConnector.setPort(port);

            /**
             * Engine
             */
            SimpleEngine simpleEngine = new SimpleEngine();
            simpleEngine.setSimpleHost(simpleHost);

            /**
             * Service
             */
            SimpleService simpleService = new SimpleService();
            simpleService.setSimpleConnector(simpleConnector);
            simpleService.setSimpleEngine(simpleEngine);

            /**
             * Server
             */
            simpleServer = new SimpleServer();
            simpleServer.setSimpleService(simpleService);

            /**
             * init
             */
            simpleServer.init();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    /**
     * 启动
     */
    public void start() throws Exception {
        this.simpleServer.start();
    }

    /**
     * Minicat 的程序启动入口
     * @param args
     */
    public static void main(String[] args) {
        Bootstrap bootstrap = new Bootstrap();
        try {
            // 初始化Minicat
            bootstrap.init();

            // 启动Minicat
             bootstrap.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
